package com.frame.util;

import java.io.*;

/**
 * <p>Title: 流操作</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2006</p>
 * <p>Company: </p>
 * @author ccs
 * @version 1.0
 */
public class StreamUtils
{
   private StreamUtils()
   {
   }

   /**
    * 将流读到字节数组中
    * @param in
    * @return
    * @throws IOException
    */
   static public byte[] streamToByte(InputStream in) throws IOException
   {
      try
      {
         if(in == null)
            return null;
         byte[] temp = new byte[8 * 1024];
         ByteArrayOutputStream out = new ByteArrayOutputStream();
         int l;
         while((l = in.read(temp)) > 0)
         {
            out.write(temp,0,l);
         }
         return out.toByteArray();
      }
      finally
      {
         try
         {
            in.close();
         }
         catch(Throwable ex)
         {
         }
      }
   }

   static public byte[] fileToByte(File file) throws IOException
   {
      return streamToByte(new FileInputStream(file));
   }

   static public byte[] fileToByte(String file) throws IOException
   {
      return streamToByte(new FileInputStream(file));
   }

   /**
    * 将字节数组写到流中
    * @param out
    * @param bytes
    * @throws IOException
    */
   static public void byteToStream(OutputStream out,byte[] data) throws
           IOException
   {
      try
      {
         int l = 8 * 1024;
         for(int i = 0; i < data.length; i += l)
         {
            if(i + l > data.length)
               l = data.length - i;
            out.write(data,i,l);
            out.flush();
         }
      }
      finally
      {
         try
         {
            out.close();
         }
         catch(Throwable ex)
         {
            ex.printStackTrace();
         }
      }
   }

   static public void streamToFile(InputStream in,String path) throws
           IOException
   {
      streamToFile(in,new File(path));
   }

   static public void streamToFile(InputStream in,File file) throws IOException
   {
      OutputStream out = null;
      try
      {
         out = new FileOutputStream(file);
         copyStream(in,out);
      }
      finally
      {
         try
         {
            if(out != null)
               out.close();
         }
         catch(Throwable ex)
         {
         }
      }
   }

   static public void copyStream(InputStream in,OutputStream out) throws
           IOException
   {
      try
      {
         if(in == null)
            return;
         byte[] temp = new byte[8 * 1024];
         int l;
         while((l = in.read(temp)) > 0)
         {
            out.write(temp,0,l);
         }
      }
      finally
      {
         try
         {
            if(in != null)
               in.close();
         }
         catch(Throwable ex)
         {
         }
      }
   }

   /**
    * 将字节数组保存到文件中
    * @param data
    * @param path
    * @throws IOException
    */
   static public void byteToFile(byte[] data,String path) throws IOException
   {
      StreamUtils.byteToStream(new FileOutputStream(path),data);
   }

   /**
    * 将字节数组保存到文件中
    * @param data
    * @param file
    * @throws IOException
    */
   static public void byteToFile(byte[] data,File file) throws IOException
   {
      File parent = file.getParentFile();
      if(!parent.exists())
         parent.mkdirs();

      StreamUtils.byteToStream(new FileOutputStream(file),data);
   }

   static public void copyFile(String source,String target) throws IOException
   {
      copyFile(new File(source),new File(target));
   }

   static public void copyFile(File source,File target) throws IOException
   {
      OutputStream out = null;
      try
      {
         File parent = target.getParentFile();
         if(!parent.exists())
            parent.mkdirs();

         out = new FileOutputStream(target);
         copyStream(new FileInputStream(source),out);
         out.flush();
      }
      finally
      {
         if(out != null)
         {
            try
            {
               out.close();
            }
            catch(Exception ex)
            {
            }
         }
      }
   }

   static public void deleteDir(File file) throws IOException
   {
      if(file.isDirectory())
      {
         File[] files = file.listFiles();
         for(int i = 0; i < files.length; i++)
         {
            File file2 = files[i];
            deleteDir(file2);
         }
      }

      file.delete();
   }

   static public void deleteFiles(File file,FileFilter filter) throws
           IOException
   {
      if(filter.accept(file))
         deleteDir(file);

      if(file.isDirectory())
      {
         File[] files = file.listFiles();
         for(int i = 0; i < files.length; i++)
         {
            File file2 = files[i];
            deleteFiles(file2,filter);
         }
      }
   }
}
